﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A78计算字符串出现的次数
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "AABBBCAABBCBCD";
            //Console.WriteLine(RepeatTimes(str,"BC")) ;
            Console.WriteLine(StringHelper. RepeatTimes1(str,"BB",false));


        }
        //第一种做法
        static int RepeatTimes(string str, string subStr)
        {
            return (str.Length - str.Replace(subStr, "").Length) / subStr.Length;
        }
    }
    class StringHelper
    {
        //第二种做法
       public  static int RepeatTimes1(string str, string subStr, bool jump = true)
        {
            int times = 0;
            for (int i = 0; i < str.Length - subStr.Length + 1; i++)
            {
                if (SameByIndex(str, subStr, i))
                {
                    times++;
                    if (jump)//是否跳过
                    {
                        i += subStr.Length - 1;
                    }
                }
            }
            return times;
        }
       private static bool SameByIndex(string str, string subStr, int index) //辅助方法
        {
            for (int i = index; i < index + subStr.Length; i++)
            {
                if (str[i] != subStr[i - index])
                {
                    return false;
                }
            }
            return true;
        }
    }
}


